package pl.s7n.paint;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class ResizeDialog extends JDialog {
  private static final long   serialVersionUID = 5204394150577294104L;

  private final JPanel        contentPanel     = new JPanel();

  private JLabel              lblCurrentHeightIs;
  private JLabel              lblCurrentWidthIs;

  private NumberFormat        formatter        = NumberFormat.getIntegerInstance();

  private JFormattedTextField tfWidth;
  private JFormattedTextField tfHeight;

  /**
   * Launch the application.
   */
  public static void main(String[] args) {
    try {
      ResizeDialog dialog = new ResizeDialog();
      dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
      dialog.setVisible(true);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * Create the dialog.
   */
  public ResizeDialog() {
    this(0, 0, "");
  }

  public ResizeDialog(Integer w, Integer h, String title) {
    setModal(true);
    setTitle(title);
    
    setBounds(100, 100, 350, 180);
    getContentPane().setLayout(new BorderLayout());
    contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
    getContentPane().add(contentPanel, BorderLayout.CENTER);
    GridBagLayout gbl_contentPanel = new GridBagLayout();
    gbl_contentPanel.columnWidths = new int[] {0, 0, 60, 0, 0, 0, 0};
    gbl_contentPanel.rowHeights = new int[] {0, 0, 0, 0, 0};
    gbl_contentPanel.columnWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
    gbl_contentPanel.rowWeights = new double[] {0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
    contentPanel.setLayout(gbl_contentPanel);
    {
      Component rigidArea = Box.createRigidArea(new Dimension(20, 20));
      GridBagConstraints gbc_rigidArea = new GridBagConstraints();
      gbc_rigidArea.insets = new Insets(0, 0, 5, 5);
      gbc_rigidArea.gridx = 0;
      gbc_rigidArea.gridy = 0;
      contentPanel.add(rigidArea, gbc_rigidArea);
    }
    {
      JLabel lblSetWidthpx = new JLabel("Set width [px]:");
      GridBagConstraints gbc_lblSetWidthpx = new GridBagConstraints();
      gbc_lblSetWidthpx.anchor = GridBagConstraints.EAST;
      gbc_lblSetWidthpx.insets = new Insets(0, 0, 5, 5);
      gbc_lblSetWidthpx.gridx = 1;
      gbc_lblSetWidthpx.gridy = 1;
      contentPanel.add(lblSetWidthpx, gbc_lblSetWidthpx);
    }
    {
      tfWidth = new JFormattedTextField(formatter);
      GridBagConstraints gbc_tfWidth = new GridBagConstraints();
      gbc_tfWidth.insets = new Insets(0, 0, 5, 5);
      gbc_tfWidth.fill = GridBagConstraints.HORIZONTAL;
      gbc_tfWidth.gridx = 2;
      gbc_tfWidth.gridy = 1;
      contentPanel.add(tfWidth, gbc_tfWidth);
    }
    {
      Component horizontalStrut = Box.createHorizontalStrut(20);
      GridBagConstraints gbc_horizontalStrut = new GridBagConstraints();
      gbc_horizontalStrut.insets = new Insets(0, 0, 5, 5);
      gbc_horizontalStrut.gridx = 3;
      gbc_horizontalStrut.gridy = 1;
      contentPanel.add(horizontalStrut, gbc_horizontalStrut);
    }
    {
      lblCurrentWidthIs = new JLabel("Current width is * px.");
      GridBagConstraints gbc_lblCurrentWidthIs = new GridBagConstraints();
      gbc_lblCurrentWidthIs.insets = new Insets(0, 0, 5, 5);
      gbc_lblCurrentWidthIs.gridx = 4;
      gbc_lblCurrentWidthIs.gridy = 1;
      contentPanel.add(lblCurrentWidthIs, gbc_lblCurrentWidthIs);
    }
    {
      JLabel lblSetHeightpx = new JLabel("Set height [px]:");
      GridBagConstraints gbc_lblSetHeightpx = new GridBagConstraints();
      gbc_lblSetHeightpx.anchor = GridBagConstraints.EAST;
      gbc_lblSetHeightpx.insets = new Insets(0, 0, 5, 5);
      gbc_lblSetHeightpx.gridx = 1;
      gbc_lblSetHeightpx.gridy = 2;
      contentPanel.add(lblSetHeightpx, gbc_lblSetHeightpx);
    }
    {
      tfHeight = new JFormattedTextField(formatter);
      GridBagConstraints gbc_tfHeight = new GridBagConstraints();
      gbc_tfHeight.insets = new Insets(0, 0, 5, 5);
      gbc_tfHeight.fill = GridBagConstraints.HORIZONTAL;
      gbc_tfHeight.gridx = 2;
      gbc_tfHeight.gridy = 2;
      contentPanel.add(tfHeight, gbc_tfHeight);
    }
    {
      lblCurrentHeightIs = new JLabel("Current height is * px.");
      GridBagConstraints gbc_lblCurrentHeightIs = new GridBagConstraints();
      gbc_lblCurrentHeightIs.insets = new Insets(0, 0, 5, 5);
      gbc_lblCurrentHeightIs.gridx = 4;
      gbc_lblCurrentHeightIs.gridy = 2;
      contentPanel.add(lblCurrentHeightIs, gbc_lblCurrentHeightIs);
    }
    {
      Component rigidArea = Box.createRigidArea(new Dimension(20, 20));
      GridBagConstraints gbc_rigidArea = new GridBagConstraints();
      gbc_rigidArea.gridx = 5;
      gbc_rigidArea.gridy = 3;
      contentPanel.add(rigidArea, gbc_rigidArea);
    }
    {
      JPanel buttonPane = new JPanel();
      buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
      getContentPane().add(buttonPane, BorderLayout.SOUTH);
      {
        JButton okButton = new JButton("OK");
        okButton.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent arg0) {
            setVisible(false);
          }
        });
        okButton.setActionCommand("OK");
        buttonPane.add(okButton);
        getRootPane().setDefaultButton(okButton);
      }
      {
        JButton cancelButton = new JButton("Cancel");
        cancelButton.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent arg0) {
            tfHeight.setValue(null);
            tfWidth.setValue(null);
            setVisible(false);
          }
        });
        cancelButton.setActionCommand("Cancel");
        buttonPane.add(cancelButton);
      }
    }

    lblCurrentWidthIs.setText(lblCurrentWidthIs.getText().replace("*", w.toString()));
    lblCurrentHeightIs.setText(lblCurrentHeightIs.getText().replace("*", h.toString()));

    tfWidth.setValue(w.longValue());
    tfHeight.setValue(h.longValue());

    this.pack();
    this.setResizable(false);
    this.setVisible(true);
  }
  
  public Point getNewSize() {
    Long width = (Long) tfWidth.getValue();
    Long height = (Long) tfHeight.getValue();
    
    if (width == null || height == null) {
      return null;
    }
    
    return new Point(width.intValue(), height.intValue());
  }

}
